package com.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.mapper.LoginDao;
import com.domain.UserLogin;
import com.service.LoginService;
import com.utils.Ret;

@Service
public class LoginServiceImlp implements LoginService {

	@Autowired
	LoginDao loginDao;

	@Override
	public Ret doLogin(String code, String ucode, String ocode, String pwd,
			HttpServletRequest request) {
		Ret ret = new Ret();

		// 验证码校验
		if ("".equals(code)) {
			ret.setStatusCode("101");
			ret.setMessage("验证码不能为空！");
		}if (ret.getMessage().length() == 0) {
			String _code = (String) request.getSession().getAttribute("captcha");
			
//			if (!code.toLowerCase().equals(_code.toLowerCase())) {
			if(false){
				ret.setStatusCode("103");
				ret.setMessage("验证码输入错误，请重新输入！");
			}else{
				UserLogin userLogin = loginDao.getUserLogin(ucode, ocode, pwd);
				if (null == userLogin) {
					ret.setMessage("输入用户信息有误！");
					ret.setStatusCode("301");
				} else {
					ret.setStatusCode("200");
					ret.setMessage("登陆成功！");
					request.getSession().invalidate();
					HttpSession newsession = request.getSession(true);
					newsession.setAttribute("usertype", userLogin.getType());
					newsession.setAttribute("usercode", userLogin.getUcode());
					newsession.setAttribute("username", userLogin.getUname());
					newsession.setAttribute("ocode", userLogin.getOcode());
				}
			}
		}

		return ret;
	}
	
	@Override
	public List<Map<String, Object>> getAuth(int type) {
		
		return loginDao.getAuth(type);
	}

}
